home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C26 / FormData.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.7 KB  |  63 lines

  1. //: C26:FormData.cpp {O}
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #include "FormData.h"
  7. #include "../require.h"
  8.  
  9. DataPair& DataPair::get(istream& in) {
  10.   first.erase(); second.erase();
  11.   string ln;
  12.   getline(in,ln);
  13.   while(ln.find("[{[") == string::npos)
  14.     if(!getline(in, ln)) return *this; // End
  15.   first = ln.substr(3, ln.find("]}]") - 3);
  16.   getline(in, ln); // Throw away [([
  17.   while(getline(in, ln))
  18.     if(ln.find("])]") == string::npos)
  19.       second += ln + string(" ");
  20.     else
  21.       return *this;
  22. }
  23.  
  24. FormData::FormData(char* fileName) {
  25.   ifstream in(fileName);
  26.   assure(in, fileName);
  27.   require(getline(in, filePath) != 0);
  28.   // Should be start of first line:
  29.   require(filePath.find("///{") == 0); 
  30.   filePath = filePath.substr(strlen("///{"));
  31.   require(getline(in, email) != 0);
  32.    // Should be start of 2nd line:
  33.   require(email.find("From[") == 0);
  34.   int begin = strlen("From[");
  35.   int end = email.find("]");
  36.   int length = end - begin;
  37.   email = email.substr(begin, length);
  38.   // Get the rest of the data:
  39.   DataPair dp(in);
  40.   while(dp) {
  41.     push_back(dp);
  42.     dp.get(in);
  43.   }
  44.  
  45. string FormData::operator[](const string& key) {
  46.   iterator i = begin();
  47.   while(i != end()) {
  48.     if((*i).first == key)
  49.       return (*i).second;
  50.     i++;
  51.   }
  52.   return string(); // Empty string == not found
  53. }
  54.  
  55. void FormData::dump(ostream& os) {
  56.   os << "filePath = " << filePath << endl;
  57.   os << "email = " << email << endl;
  58.   for(iterator i = begin(); i != end(); i++)
  59.     os << (*i).first << " = " 
  60.        << (*i).second << endl;
  61. } ///:~
  62.